import i18n from 'i18next';
import { copyPart, transformPart } from '../common/part-and-transform/funcs';
import { PartAndTransformDialog } from '../common/part-and-transform/PartAndTransformDialog';
import { PartAndTransformCmdParams } from '../common/part-and-transform/types';

async function finishCopy({
  formData,
  matrix,
  onClose,
  docTree,
}: PartAndTransformCmdParams) {
  const part = docTree.children.find(
    ({ key }) => key === formData.select?.idInfo?.o,
  );

  await transformPart(await copyPart(part, docTree), formData, matrix);

  onClose();
}

export const cmdCopyPart = {
  id: 'CmdCopyPart',
  label: 'command.copy-part-or-product',
  icon: 'commands/product/copy',
  create: (label?: string, onClose?: () => void) => (
    <PartAndTransformDialog
      key={cmdCopyPart.id}
      title={i18n.t(label)}
      onClose={onClose}
      executeCmd={finishCopy}
    />
  ),
};
